package sample.plugin.http.file;

import com.rapid.api.project.structure.PSCheckedNewChildObject;
import com.rapid.api.project.structure.PSFile;
import com.rapid.api.project.structure.extension.PSObjectCreationForm;
import com.rapid.api.project.structure.extension.PSObjectCreationOption;
import com.rapid.framework.data.form.Form;

import java.io.File;
import java.io.IOException;

public class CreateRequestFileOption implements PSObjectCreationOption {

    @Override
    public String getName() {
        return "新建网络请求";
    }

    @Override
    public void create(PSCheckedNewChildObject object, Form form, OnCreatePSObjectListener listener) {
        if (!(object instanceof PSFile)) {
            listener.onFail("仅支持在文件夹下新建");
            return;
        }
        String name = PSObjectCreationForm.getFileName(form);
        File parent = ((PSFile) object).getFile();
        File file = new File(parent, name + ".req");
        try {
            file.createNewFile();
            listener.onSuccess(new PSFile(file));
        } catch (IOException e) {
            e.printStackTrace();
            listener.onFail("创建文件失败");
        }
    }
}
